package ldh.im.fxgui.component.cell;

import com.jfoenix.controls.JFXButton;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import ldh.im.fxbase.data.pojo.ImMessage;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxbase.util.ImCacheFactory;
import ldh.im.fxgui.handle.IMessageShowHandler;
import ldh.im.fxgui.pane.MessagePaneEnum;
import ldh.im.fxgui.util.UserHeadImageUtil;
import ldh.im.fxgui.pojo.SearchMessage;
import ldh.im.util.DateUtil;

/**
 * 查看历史信息的: ListCell
 */
public class SearchMessageView extends GridPane {

    private IMessageShowHandler messageShowHandler;
    private SearchMessage searchMessage;
    private JFXButton viewContentBtn;

    public SearchMessageView(IMessageShowHandler messageShowHandler, SearchMessage searchMessage) {
        this.messageShowHandler = messageShowHandler;
        this.searchMessage = searchMessage;

        this.getStyleClass().add("message-pane");

        initUi();
        bindEvent();
    }

    private void initUi() {
        ImUser fromImUser = ImCacheFactory.getInstance().getUser(searchMessage.getImMessage().getFromUserId());

        // 图像
        ImageView userHeadImageView = new ImageView();
        userHeadImageView.setFitHeight(40);
        userHeadImageView.setFitWidth(40);
        userHeadImageView.setImage(UserHeadImageUtil.getHeadImage(fromImUser.getHeadImg()));

        // 名称
        Label userNameLabel = new Label();
        userNameLabel.getStyleClass().add("user-name");
        userNameLabel.setText(fromImUser.getUserName());

        // 消息时间
        Label dateLabel = new Label(DateUtil.format(searchMessage.getImMessage().getServerReceiveTime(), "yyyy-MM-dd HH:mm:ss"));
        dateLabel.getStyleClass().add("date");

        // 消息内容
        MessagePaneEnum messagePaneEnum = MessagePaneEnum.getByMessageType(searchMessage.getImMessage().getMessageType());
        Node contentPane = messagePaneEnum.getMessagePane().createViewPane(null, searchMessage.getImMessage(), true);

        // 查看上下文按钮
        viewContentBtn = new JFXButton("查看上下文");
        viewContentBtn.getStyleClass().add("view-content");
        viewContentBtn.setVisible(false);

        // 布局
        GridPane.setConstraints(userHeadImageView, 0, 0, 1, 2);
        GridPane.setConstraints(userNameLabel, 1, 0, 1, 1);
        GridPane.setConstraints(dateLabel, 2, 0, 1, 1);
        GridPane.setConstraints(contentPane, 1, 1, 1, 1);
        GridPane.setConstraints(viewContentBtn, 2, 1, 1, 2);

        ColumnConstraints c1 = new ColumnConstraints(50);
        c1.setHalignment(HPos.CENTER);
        ColumnConstraints c2 = new ColumnConstraints();
        c2.setHalignment(HPos.LEFT);
        c2.setHgrow(Priority.ALWAYS);

        ColumnConstraints c3 = new ColumnConstraints(120);
        c3.setHalignment(HPos.LEFT);

        RowConstraints r1 = new RowConstraints();
        r1.setValignment(VPos.TOP);
        RowConstraints r2 = new RowConstraints();

        this.getChildren().addAll(userHeadImageView, userNameLabel, dateLabel, contentPane, viewContentBtn);
        this.getColumnConstraints().addAll(c1, c2, c3);
        this.getRowConstraints().addAll(r1, r2);
    }

    private void bindEvent() {
        this.setOnMouseEntered(e->{
            if (searchMessage.isSearchable()) {
                viewContentBtn.setVisible(true);
            }
        });
        this.setOnMouseExited(e->{
            if (searchMessage.isSearchable()) {
                viewContentBtn.setVisible(false);
            }
        });

        viewContentBtn.setOnAction(e->{
            if (!searchMessage.isSearchable()) return;
            showMessage(searchMessage.getImMessage());
        });
    }

    private void showMessage(ImMessage imMessage) {
        messageShowHandler.showMessage(imMessage);
    }
}
